package com.foreveross.proxyip.web.action.pools;

import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.dayatang.domain.InstanceFactory;
import com.foreveross.proxyip.application.pool.ChannelPoolApplication;
import com.foreveross.proxyip.application.pool.vo.ChannelPoolVo;
import com.foreveross.proxyip.core.BaseCode;
import com.foreveross.proxyip.core.pools.PoolsManager;
import com.foreveross.proxyip.core.redis.PiRedisExecption;
import com.foreveross.proxyip.entity.ChannelEntity;
import com.foreveross.proxyip.exception.ProxyIpServiceException;
import com.foreveross.proxyip.infra.PageData;
import com.foreveross.proxyip.infra.conf.PropertyUtils;
import com.foreveross.proxyip.infra.conf.proxyip.ProxyIpPropertiesLoader;
import com.foreveross.proxyip.util.Config;
import com.foreveross.proxyip.web.action.BaseAction;
@SuppressWarnings("serial")
@Controller
@org.springframework.context.annotation.Scope("prototype")
@Namespace("/pools/channelPool")
@ParentPackage("json-default")
public class ChannelPoolAction  extends BaseAction {
	
	@Autowired 
	ChannelPoolApplication application;

	PageData pageData = new PageData();
	
	private Integer pageIndex;

	private Integer pageLen;
	
	ChannelPoolVo vo = new ChannelPoolVo();
	
	@Action(value="toJsp",results={@Result(name="dispatcher", type="dispatcher",location="/pools/channelPool.jsp")})
	public String toJsp()throws ProxyIpServiceException{
		loadChannels();
		return "dispatcher";
	}
	
	/**
	 * 检索（按某些条件）
	 * @return
	 */
	@Action(value="search", results={@Result(name="JSON", type="json", params={"root","pageData"})})
	public String search()throws Exception{
		//页面加载好pageData.PageIndex ,pageData.PageLen
		pageData.setPageIndex(pageIndex);
		pageData.setPageLen(pageLen);
		application.loadPageData(pageData,vo.getChannelCode());
//		initPoolVo();
		return "JSON";
	}
	
	private void initPoolVo() throws NumberFormatException, PiRedisExecption {
		vo.setCurrNum(pageData.getDataSize());
		Integer threadMaxIpCount = Integer.parseInt( BaseCode.getValue("CHANNEL_THREAD_MAX_COUNT"));
		Long threadCount = vo.getCurrNum()/threadMaxIpCount;
		if(threadCount != 0){
		threadCount = vo.getCurrNum()%threadMaxIpCount!=0?threadCount+1:threadCount;
		}else{
			threadCount = 1L;
		}
		vo.setThreadCount(threadCount);
	}
	
	private void loadChannels(){
		PoolsManager poolsManager = InstanceFactory
		.getInstance(PoolsManager.class);
		List<ChannelEntity> channels = poolsManager.getChannelEntitys();
		vo.setChannels(channels);
	}

	public PageData getPageData() {
		return pageData;
	}

	public void setPageData(PageData pageData) {
		this.pageData = pageData;
	}

	public Integer getPageIndex() {
		return pageIndex;
	}

	public void setPageIndex(Integer pageIndex) {
		this.pageIndex = pageIndex;
	}

	public Integer getPageLen() {
		return pageLen;
	}

	public void setPageLen(Integer pageLen) {
		this.pageLen = pageLen;
	}

	public ChannelPoolVo getVo() {
		return vo;
	}

	public void setVo(ChannelPoolVo vo) {
		this.vo = vo;
	}
	
	
	
}
